import { $t } from '@/locales';

/**
 * Transform record to option
 *
 * @example
 *   ```ts
 *   const record = {
 *     key1: 'label1',
 *     key2: 'label2'
 *   };
 *   const options = transformRecordToOption(record);
 *   // [
 *   //   { value: 'key1', label: 'label1' },
 *   //   { value: 'key2', label: 'label2' }
 *   // ]
 *   ```;
 *
 * @param record
 */
export function transformRecordToOption<T extends Record<string, string>>(record: T) {
  return Object.entries(record).map(([value, label]) => ({
    value,
    label
  })) as CommonType.Option<keyof T>[];
}

/**
 * Translate options
 *
 * @param options
 */
export function translateOptions(options: CommonType.Option<string>[]) {
  return options.map(option => ({
    ...option,
    label: $t(option.label as App.I18n.I18nKey)
  }));
}

export function isBusinessYse(value:string|undefined):boolean{
  return value == '0';
}

/** 设置对象数据 */
export function objectAssign<T extends Record<string, any>>(target: T, source: Partial<T>) {
  Object.assign(target, source);
}

/**
 * 重置对象属性
 * @param target 目标对象
 * @param def 默认属性
 * @returns 重置后的对象
 */
export function resetObject<T>(target:T,def?:T):T{
  const value  = def||{}
	//@ts-ignore
  Object.keys(target).forEach((key:string)=>{
		//@ts-ignore
     target[key] = value[key] || null;
  })
  return target;
}

export function obj2any(arr:any[]|undefined):any[]|undefined{
	if(!arr){
		return undefined;
	}
	return arr;
}
